<?php

if(! isset($redirect_uri))
{
	$redirect_uri = 'http://www.dannetwork.com/verify/login?sc=gp';
}

$client = new Google_Client();
$client->setApplicationName("Aplikasi Dadan Network di Google");
// Visit https://code.google.com/apis/console?api=plus to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
// $client->setClientId('insert_your_oauth2_client_id');
// $client->setClientSecret('insert_your_oauth2_client_secret');
// $client->setRedirectUri('insert_your_redirect_uri');
// $client->setDeveloperKey('insert_your_developer_key');
$client->setClientId('1037184364157.apps.googleusercontent.com');
$client->setClientSecret('Ny_TYYSI42Dm3BcpW9Gu4st5');
$client->setRedirectUri($redirect_uri);
$oauth2 = new Google_Oauth2Service($client);

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  $redirect = $redirect_uri;
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  return;
}

if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['token']);
  $client->revokeToken();
}

if ($client->getAccessToken()) {
  $gpuser = $oauth2->userinfo->get();

  // The access token may have been updated lazily.
  $_SESSION['token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
}

?>
